package com.gali.rabbit.five;

import com.gali.rabbit.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: gali
 * @Date: 2022-08-02 16:00
 * @Description:消息的接收
 */
public class ReceiveLogs01 {
    //交换机的名称
    public static final String EXCHANGE_NAME="logs";
    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        //声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");//发布/订阅模式
        //声明一个队列 临时队列 断开连接时，队列就自动删除了
        String queue = channel.queueDeclare().getQueue();
        //绑定交换机与队列
        channel.queueBind(queue,EXCHANGE_NAME,"");
        System.out.println("ReceiveLogs01等待接收消息，把接收到的消息打印在屏幕上......");
        //接收消息
        DeliverCallback deliverCallback=(consumerTag,message)->{
            System.out.println("控制台打印接收到的消息："+new String(message.getBody(),"UTF-8"));
        };
        //取消接收消息
        channel.basicConsume(queue,true,deliverCallback,consumerTag->{});
    }
}
